<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>学生信息管理</title>
	</head>
	<body>
		
		<h1>学生信息管理</h1>
		 
		<a href="/student/add">添加学生信息</a>
		<table border="1px solid #ccc" cellspacing="2px" cellpadding="2px">
			 <thead>
			 	<th>序号</th>
			 	<th>姓名</th>
			 	<th>年龄</th>
			 	<th>专业</th>
			 	<th>性别</th>
			 	<th>电话</th>
			 	<th>学号</th>
			 	<th>籍贯</th>
			 	<th>操作</th>
			 </thead>
			<tbody>
				
			</tbody>
			
			
			
			
		</table>
		<a  id="next">下一页</a>
		<script type="text/template" id="temp">
		{{each students  value i}}
			<tr>
			<td>{{i+1}}</td>
			<td>{{value.name}}</td>
			<td>{{value.age}}</td>
			<td>{{value.major}}</td>
			<td>{{value.sex}}</td>
			<td>{{value.tel}}</td>
			<td>{{value.stuid}}</td>
			<td>{{value.home}}</td>
			<td>
				<a href="/student/edit?id={{value._id}}">编辑</a>
				<a href="/student/remove?id={{value._id}}" class="delect">删除</a>
			</td>
			</tr>
		{{/each}}	
		</script>
		
	<script src="/jquery/dist/jquery.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="/art-template/lib/template-web.js" type="text/javascript" charset="utf-8"></script>	
		<script type="text/javascript">
			
		    function getlist(currentPage){
					$.ajax({
						type:"get",
						url:"/student/list",
						data:{currentPage:currentPage},
						success:function(data){
							if(data.err_num === 1){
								window.alert('服务器链接失败')
							}else if(data.err_num ===0 ){
								var $html = template('temp',{students:data.messages})
								$('tbody').append($html)						
							}else if(data.err_num==2){
								totle = currentPage
								window.alert('没有数据了')
							}
						}
					});
		    }
		    getlist(1);
		    var currentPage = 1
		        totle = 0
			$("#next").on('click',function(){
				if(totle === currentPage) {
					$(this).css('color','red')
					return
				}
					
				currentPage++;
				getlist(currentPage)
			})
			$('body').on('click','.delect',function(e){
				if(window.confirm("确定要删除吗？")){
//					e.preventDefault()
					$.ajax({
						type:"get",
						url:$(this).attr('href'),
						dataType:'json',
						success:function(data){
							switch (data.err_num){
								case 1:
								    window.alert("删除数据失败")
									break;
								case 0:
								    window.alert("删除数据成功")
									break;
								default:window.alert("未知错误")
									break;
							}
						}
					});
					return false;
				}
				return false;

				
			
			})
			
			
			
		</script>
		
		
		
		
		
	</body>
</html>
